package com.zxx.retrofit.net;


import android.util.Log;

import com.zxx.retrofit.BuildConfig;

import java.io.File;
import java.io.IOException;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Description: RetrofitHelper
 * Creator: yxc
 * date: 2016/9/21 10:03
 */
public class RetrofitHelper {


    private static BombApi bombApi;
    private static DoubanApi doubanApi;

    private static Retrofit retrofit;
    private static final int DEFAULT_TIMEOUT = 5;

    private static OkHttpClient okHttpClient = null;


    public static BombApi getBombLoinApi() {
        initOkHttp();
        if (bombApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(BombApi.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
            bombApi = retrofit.create(BombApi.class);
        }
        return bombApi;
    }

    public static DoubanApi getDoubanApi() {
        initOkHttp();
        if (doubanApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(DoubanApi.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    //增加返回值为String的支持
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
            doubanApi = retrofit.create(DoubanApi.class);
        }
        return doubanApi;
    }

    private static void initOkHttp() {
        if (okHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.addInterceptor(BOMB_RESPONSE_INTERCEPTOR);
            //NONE BASIC  HEADERS  BODY
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
            builder.addInterceptor(loggingInterceptor);
            okHttpClient = builder.build();
        }
    }

    private static final Interceptor BOMB_RESPONSE_INTERCEPTOR = new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {

            Request request = chain.request();
            request = request.newBuilder()
                    .addHeader("Content-Type", "application/json")
//                    .addHeader("X-Bmob-Application-Id", BombApi.applicationId)
//                    .addHeader("X-Bmob-REST-API-Key", BombApi.resetApiKey)
                    .build();
            return chain.proceed(request);
        }
    };

    private static final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            //打印retrofit日志
            Log.i("RetrofitLog","retrofitBack = "+message);
        }
    });
}
